// Learn TypeScript:
//  - https://docs.cocos.com/creator/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/manual/en/scripting/life-cycle-callbacks.html

import BaseView from "../base/BaseView";
import ThsListView from "../base/ThsListView";
import LogModule from "../module/LogModule";
import TushareModule from "../module/TushareModule";
import DropList from "./items/DropList";
import {PlateEnumType, PlateNames, ThsPlate, ThsRankData} from "./models/ThsRankData";
import ColorConfig from "./models/ColorConfig";
import TimeModule from "../module/TimeModule";

const {ccclass, property} = cc._decorator;

@ccclass
export default class ThsGatherView extends BaseView {

    @property(cc.Label)
    startDate: cc.Label = null;

    @property(cc.Label)
    endDate: cc.Label = null;

 
    @property(DropList)
    dropPlateEnumType: DropList = null;

    @property(DropList)
    dropFileType: DropList = null;

    @property(ThsListView)
    thsListView: ThsListView = null;

    thsGatherData: any = {};

    thsRank: ThsRankData = null;

    onLoad () {
        ColorConfig.getModule().load();
        this.dropPlateEnumType.setData(PlateNames, 0);
        this.dropFileType.setData(['csv', 'json', 'txt'], 0);
        this.dropPlateEnumType.setSelectCallback((index)=>{
            this.updateThsLayer();
        });
        this.thsGatherData.industryData = [];
        this.thsGatherData.subtradeData = [];
        this.thsGatherData.conceptData = [];
        this.thsGatherData.industryCount = 20;
        this.thsGatherData.subtradeCount = 20;
        this.thsGatherData.conceptCount = 20;
        cc.director.on('ClickStockDate', this.onClickStockDate, this);
        cc.director.on('ClickStockItem', this.onClickStockItem, this);
        this.thsRank = ThsRankData.getInstance();

        let startDate = cc.sys.localStorage.getItem('Label_' + this.startDate.node.name);
        let endDate = cc.sys.localStorage.getItem('Label_' + this.endDate.node.name);
        if (startDate) {
            this.startDate.string = startDate;
        } else {
            this.startDate.string = TimeModule.getInstance().preTradeDate();
        }
        if (endDate) {
            this.endDate.string = endDate;
        } else {
            this.endDate.string = TimeModule.getInstance().strToday();
        }
    }

    protected onDestroy(): void {
        cc.director.off('ClickStockDate', this.onClickStockDate, this);
        cc.director.off('ClickStockItem', this.onClickStockItem, this);
    }

    onClickStockDate () {

    }

    onClickStockItem (stockInfo) {
        if (this.thsRank.isFinish() == false) {
            this.showToast('等待数据拉取完成');
            return;
        }
        let type = this.dropPlateEnumType.getSelectIndex();
        stockInfo.plateType = type;
        this.createDialog(BaseView.Views.LookNodeView, stockInfo);
    }

    onBtnStartDate () {
        this.createDialog(BaseView.Views.CalendarView, this.startDate);
    }

    onBtnEndDate () {
        this.createDialog(BaseView.Views.CalendarView, this.endDate);
    }

    onBtnLookRank () {
        if (this.thsRank.isFinish() == false) {
            this.showToast('等待数据拉取完成');
            return;
        }
        this.thsListView.node.off(cc.Node.EventType.MOUSE_WHEEL);
        let type = this.dropPlateEnumType.getSelectIndex();
        let list = this.thsRank.getPlatelist(type);
        let item = list[0];
        item.plateType = type;
        this.createDialog(BaseView.Views.RankTrendView, list[0]);
    }

    onBtnLoadData () {
        let start = this.startDate.string;
        let end = this.endDate.string;
        let startNumber = parseInt(start);
        let endNumber = parseInt(end);
        if (startNumber > endNumber) {
            this.showToast('结束时间不能早于开始时间');
            return;
        }
        this.thsGatherData.industryData = [];
        this.thsGatherData.conceptData = [];
        this.thsGatherData.subtradeData = [];
        this.thsRank.clearData();
        let module = TushareModule.getInstance();
        module.loadThsData(startNumber, endNumber, (event: string, data: any) => {
            this.onTushareEvent(event, data);
        });
    }

    onBtnSaveFile () {
       
    }

    onBtnCompare () {
        let type = this.dropPlateEnumType.getSelectIndex();
        let list = this.thsRank.getPlatelist(type);
        let item1 = list[0];
        let item2 = list[1];
        this.createDialog(BaseView.Views.PlateCompareView, {item1: item1, item2: item2});
    }

    onTushareEvent (event: string, data: any) {
        if (event == 'LoadStart') {
            this.thsGatherData.industryCount = data.industryCount;
            this.thsGatherData.subtradeCount = data.subtradeCount;
            this.thsGatherData.conceptCount = data.conceptCount;
            this.thsRank.setDataCount(data);
            this.updateThsLayer();
        } else if (event == 'LoadFailed') {
            this.showToast(data.msg);
        } else if (event == 'LoadOver') {
            this.showToast('拉取数据完成');
            LogModule.log('拉取数据完成');
            this.thsRank.finish();
        } else {
            LogModule.log('拉取到:' + data.date);
            this.thsRank.pushData(data);
            this.pushThsData(PlateEnumType.Industry, data);
            this.pushThsData(PlateEnumType.Subtrade, data);
            this.pushThsData(PlateEnumType.Concept, data);
        }
    }

    pushThsData (type, data) {
        let listData = [];
        let key = '';
        if (type == PlateEnumType.Industry) {
            key = ThsPlate.Industry;
        } else if (type == PlateEnumType.Subtrade) {
            key = ThsPlate.Subtrade;
        } else if (type == PlateEnumType.Concept) {
            key = ThsPlate.Concept;
        }
    
        let keyData = data[key];
        let date = data.date;
        listData.push({date: date});
        for (let j = 0; j < keyData.length; j++) {
            const item = keyData[j];
            listData.push(item);
        }

        this.thsGatherData[key + 'Data'] = this.thsGatherData[key + 'Data'].concat(listData);
        let selectType = this.dropPlateEnumType.getSelectIndex();
        if (selectType == type) {
            this.thsListView.pushData(listData);
        }
    }

    //刷新
    updateThsLayer () {
        let type = this.dropPlateEnumType.getSelectIndex();
        let list = [];
        let count = 0;
        if (type == PlateEnumType.Industry) {
            list = this.thsGatherData.industryData;
            count = this.thsGatherData.industryCount;
        } else if (type == PlateEnumType.Subtrade) {
            list = this.thsGatherData.subtradeData;
            count = this.thsGatherData.subtradeCount;
        } else if (type == PlateEnumType.Concept) {
            list = this.thsGatherData.conceptData;
            count = this.thsGatherData.conceptCount;
        }
        this.thsListView.setData(list, count + 1);
    }
}
